from django.test import TestCase
from extremeKanban.kanban.exception import UnknownMethodException
from extremeKanban.kanban.server import KanbanServer, KanbanService
from extremeKanban.kanban.models import Story

class ToPublish(object):
	def __init__(self):
		self._calls = []

	def polop(self):
		self._calls.append("polop")
		return "polop called"

	def echo(self, *args):
		return args

	def _pilip(self):
		self._calls.append("pilip")

class ServerTest(TestCase):
	def test_constructor(self):
		"""KanbanServer : Can create a server"""
		KanbanServer()

	def test_publish(self):
		"""KanbanServer : Can publish an object, all public methods are available"""
		server = KanbanServer()
		server.publish(ToPublish())
		self.failUnlessEqual(["echo", "polop"], server.methods)

	def test_invoke(self):
		"""KanbanServer : Can invoke a method without args"""
		server = KanbanServer()
		service = ToPublish()
		server.publish(service)
		result = server.invoke("polop")
		self.failUnlessEqual("polop called", result)
		self.failUnlessEqual(["polop"], service._calls)

	def test_invoke_with_args(self):
		"""KanbanServer : Can invoke a method with args"""
		server = KanbanServer()
		service = ToPublish()
		server.publish(service)
		result = server.invoke("echo", 1, 2, "polop")
		self.failUnlessEqual((1, 2, "polop"), result)

	def test_invoke_unknown_method(self):
		"""KanbanServer : Invoke unknown method raises exception"""
		server = KanbanServer()
		server.publish(ToPublish())
		try:
			server.invoke("DTC", 1, 2, "polop")
		except UnknownMethodException, e:
			self.failUnlessEqual("Unknown method : DTC", str(e))

class KanbanServiceTest(TestCase):

	def test_create_story(self):
		"""KanbanService : Create a new story"""
		service = KanbanService()
		self.failUnlessEqual(0, len(Story.objects.all()))
		id = service.create_story()
		self.failUnlessEqual(1, len(Story.objects.all()))
		self.failUnlessEqual(id, Story.objects.all()[0].id)

